package fileutil

import (
	"bufio"
	"io"
	"os"
)

//read  lines
func EachLine(reader io.Reader, callFunc func(line string)) error {
	bufReader := bufio.NewReader(reader)
	for {
		lineBytes, _, err := bufReader.ReadLine()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		} else {
			callFunc(string(lineBytes))
		}
	}
	return nil
}

//read file lines
func EachLineFile(filepath string, callFunc func(line string)) error {
	file, err := os.Open(filepath)
	if err != nil {
		return err
	}
	return EachLine(file, callFunc)
}
